#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <vector>
using namespace std;

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n;
	int flag = 0; //表示存不存在
	cin >> n;
	vector<string>ops(n),s; //表示有几行数组 s表示数组模拟栈
	string target;


	for (int i = 0; i < n; i++) cin >> ops[i];
	cin >> target;
	for (int i = 0; i < n; i++)
	{
		if (target == ops[i])
		{
			flag = 1;
			s.push_back(ops[i]);
			break;
		}
		if (ops[i] == "return") s.pop_back();
		else s.push_back(ops[i]);
	}
	if (flag == 0) printf("NOT REFERENCED\n");
	else
	{
		for (int i=0;i<s.size();i++)
		{
			if (i) cout << "->" << s[i];
			else cout << s[i];
		}
	}

	return 0;
}